#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>

static GtkImage                *image = NULL;
static GtkWidget               *window;
static GtkWidget               *entry;

typedef struct
{
  GtkBuilder *builder;
  gpointer user_data;
} SGlobalData;


void callback_about (GtkMenuItem *menuitem, gpointer data)
{
  GtkWidget *dialog = NULL;
  
  dialog =  gtk_about_dialog_new ();
  /*gtk_about_dialog_set_authors(GTK_ABOUT_DIALOG(dialog),"thongp_j kaplan_v pan_c  kientz_a" );*/
  gtk_about_dialog_set_program_name(GTK_ABOUT_DIALOG(dialog), "bit à bit");
  gtk_about_dialog_set_version  (GTK_ABOUT_DIALOG(dialog),"0.1");
  
  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_hide (dialog);
}

static void cb_changed(GtkComboBox *combo, gpointer data)
{
    /* Obtain currently selected string from combo box */
    gchar *string = gtk_combo_box_get_active_text(combo);

    /* Print it to the console - if nothing is selected, print NULL */
    g_print("Selected (simple): >> %s <<\n", (string ? string : "NULL"));

    /* Free string */
    g_free(string);
}

void callback_close(GtkObject *object, gpointer data){
  gtk_main_quit();
}

void on_mainWindow_destroy(GtkObject *object, gpointer data){
  gtk_main_quit();
}

void callback_open (GtkWidget *widget, gpointer data)
{
  GtkWidget *dialog = NULL;
  if (!dialog) {
  dialog = gtk_file_chooser_dialog_new("Open file", GTK_WINDOW(window),
				       GTK_FILE_CHOOSER_ACTION_OPEN,
				       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
				       GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
				       NULL);
  }
  if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
    char *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
    gtk_image_set_from_file(GTK_IMAGE(image),file);
    gtk_entry_set_text(GTK_ENTRY(entry),file);
  }
  gtk_widget_hide(dialog);
}

int main (int argc, char *argv[])
{

  SGlobalData              data; 
  GtkBuilder              *builder;
  GtkWidget               *combo;
  GError                  *error = NULL;
  
  gtk_init (&argc, &argv);
  builder = gtk_builder_new();
  gtk_builder_add_from_file (builder, "interface.xml", &error);
  if (error)
    {
      gint code = error->code;
      g_printerr("%s\n", error->message);
      g_error_free (error);
      return code;
    }
  
  window = GTK_WIDGET (gtk_builder_get_object (builder, "mainWindow"));
  gtk_builder_connect_signals (builder, &data);          
  g_object_unref (G_OBJECT (builder));
  entry = gtk_entry_new();
  image = gtk_image_new_from_file("logo.gif");
  combo = gtk_combo_box_new_text();
  gtk_widget_show (window);
  gtk_main ();
  
  g_signal_connect( G_OBJECT( combo ), "changed",
		    G_CALLBACK( cb_changed ), NULL );
  
  return 0;

}
